﻿using System;
using System.Text;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;

namespace EmailMarketingCampaign
{
    public partial class testAddRow : System.Web.UI.Page
    {
        public int RowsOfTable =1;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Session["RowsOfTable"] = RowsOfTable;
            }
            else
            {
                int countRows=Convert.ToInt32(Session["RowsOfTable"]);               
                for (int i = 1; i < countRows; i++)
                {
                    TableRow tRow = new TableRow();
                    // Add first column
                    TableCell tCell1 = new TableCell();
                    CheckBox cb = new CheckBox();
                    // Set a unique ID for each TextBox added
                    cb.ID = "ckbTable" + i;
                    // Add the control to the TableCell
                    tCell1.Controls.Add(cb);
                    // Add the TableCell to the TableRow
                    tRow.Cells.Add(tCell1);

                    // Add second column
                    TableCell tCell2 = new TableCell();
                    Label lbDate = new Label();
                    lbDate.Text = "Date" + i + ":";
                    tCell2.Controls.Add(lbDate);
                    tRow.Cells.Add(tCell2);

                    // Add third column
                    TableCell tCell3 = new TableCell();
                    TextBox tb = new TextBox();
                    tb.ID = "txtDate" + i;
                    //tCell3.Text = "<script language=\"JavaScript\">new tcal({'formname': 'aspnetForm','controlname': 'ctl00$ContentPlaceHolder1$txtDate'})</script>";
                    tCell3.Controls.Add(tb);
                    tRow.Cells.Add(tCell3);

                    // Add fourth column
                    TableCell tCell4 = new TableCell();
                    Label lbTime = new Label();
                    lbTime.Text = "Time" + i + ":";
                    tCell4.Controls.Add(lbTime);
                    tRow.Cells.Add(tCell4);

                    // Add fifth column
                    TableCell tCell5 = new TableCell();
                    DropDownList ddlHour = new DropDownList();
                    ddlHour.ID = "ddlHours" + i;
                    ddlHour.Items.Add("1");
                    ddlHour.Items.Add("2");
                    ddlHour.Items.Add("3");
                    tCell5.Controls.Add(ddlHour);

                    DropDownList ddlMinute = new DropDownList();
                    ddlMinute.ID = "ddlMinutes" + i;
                    ddlMinute.Items.Add("00");
                    ddlMinute.Items.Add("01");
                    ddlMinute.Items.Add("02");
                    tCell5.Controls.Add(ddlMinute);

                    DropDownList ddlType = new DropDownList();
                    ddlType.ID = "ddlType" + i;
                    ddlType.Items.Add("AM");
                    ddlType.Items.Add("PM");
                    tCell5.Controls.Add(ddlType);
                    tRow.Cells.Add(tCell5);

                    dataTable.Rows.Add(tRow);

                }
                Session["RowsOfTable"] = countRows;
            }
            //Label1.Text = dataTable.Rows.Count.ToString();
            //Label2.Text = RowsOfTable.ToString();
        }


        protected void CsharpVoid()
        {
            int strCC = RowsOfTable;
            Response.Write(strCC);

        }


        protected void AddRow_Click(object sender, EventArgs e)
        {
            RowsOfTable = Convert.ToInt32(Session["RowsOfTable"]);
            int rows = Convert.ToInt32(Session["RowsOfTable"])+1;
            for (int i = RowsOfTable; i < rows; i++)
            {
                TableRow tRow = new TableRow();
                // Add first column
                TableCell tCell1 = new TableCell();
                CheckBox cb = new CheckBox();
                // Set a unique ID for each TextBox added
                cb.ID = "ckbTable" + i;
                // Add the control to the TableCell
                tCell1.Controls.Add(cb);
                // Add the TableCell to the TableRow
                tRow.Cells.Add(tCell1);

                // Add second column
                TableCell tCell2 = new TableCell();
                Label lbDate = new Label();
                lbDate.Text = "Date" + i + ":";
                tCell2.Controls.Add(lbDate);
                tRow.Cells.Add(tCell2);

                // Add third column
                TableCell tCell3 = new TableCell();
                TextBox tb = new TextBox();
                tb.ID = "txtDate" + i;
                tb.EnableViewState = true;
                tCell3.Controls.Add(tb);
                tRow.Cells.Add(tCell3);

                // Add fourth column
                TableCell tCell4 = new TableCell();
                Label lbTime = new Label();
                lbTime.Text = "Time" + i + ":";
                tCell4.Controls.Add(lbTime);
                tRow.Cells.Add(tCell4);

                // Add fifth column
                TableCell tCell5 = new TableCell();
                DropDownList ddlHour = new DropDownList();
                ddlHour.ID = "ddlHours" + i;
                ddlHour.Items.Add("1");
                ddlHour.Items.Add("2");
                ddlHour.Items.Add("3");
                tCell5.Controls.Add(ddlHour);

                DropDownList ddlMinute = new DropDownList();
                ddlMinute.ID = "ddlMinutes" + i;
                ddlMinute.Items.Add("00");
                ddlMinute.Items.Add("01");
                ddlMinute.Items.Add("02");
                tCell5.Controls.Add(ddlMinute);

                DropDownList ddlType = new DropDownList();
                ddlType.ID = "ddlType" + i;
                ddlType.Items.Add("AM");
                ddlType.Items.Add("PM");
                tCell5.Controls.Add(ddlType);
                tRow.Cells.Add(tCell5);

                dataTable.Rows.Add(tRow);
            }
            Session["RowsOfTable"] = rows;
        }


        protected void DeleteRow_Click(object sender, EventArgs e)
        {
            //Table table = (Table)Page.Form.FindControl("dataTable");
            //Label1.Text = dataTable.Rows.Count.ToString();
            int RowsOfTable = dataTable.Rows.Count;
            int DeleteRows = 0;
            string rowNumber = "";
            for (int i = 0; i < dataTable.Rows.Count; i++)
            {
                //string check = Request.Form["ckbTable" + i];
                //CheckBox ck = dataTable.Rows[i].Cells[0].Controls[0] as CheckBox;
                CheckBox ck = (CheckBox)dataTable.Rows[i].Cells[0].FindControl("ckbTable" + i);
                
                if (ck.Checked == true)
                {
                    //dataTable.Rows.RemoveAt(i);
                    rowNumber += i + ",";
                    DeleteRows++;
                }
            }

            string[] rn = new string[DeleteRows];
            char[] splitterRows = { ',' };
            rn = rowNumber.Split(splitterRows);
            for (int i = rn.Length - 1; i >=0; i--)
            {
                if (rn[i] == null || rn[i] == "")
                {

                }
                else
                {
                    int at = Convert.ToInt32(rn[i]);
                    dataTable.Rows.RemoveAt(at);
                }
            }

            Session["RowsOfTable"] = RowsOfTable - DeleteRows;

            //Label1.Text = dataTable.Rows.Count.ToString();
            //Label2.Text = RowsOfTable.ToString();
        }

        protected void send_Click(object sender, EventArgs e)
        {
            string[] date=new string[10];
            string[] hh = new string[10];
            string[] mm = new string[10];
            string[] type = new string[10];
            string[] datetime = new string[10];
            DateTime[] dt = new DateTime[10];
            string[] strdt = new string[10];
            for (int i = 0; i < dataTable.Rows.Count; i++)
            {
                date[i] = ((TextBox)dataTable.Rows[i].Cells[2].FindControl("txtDate" + i)).Text.ToString();

                string[] newDate = new string[3];
                char[] splitter = { '/' };
                newDate = date[i].Split(splitter);
                string month = newDate[0];
                string days = newDate[1];
                string year = newDate[2];
                date[i] = days + "/" + month + "/" + year;


                hh[i] = ((DropDownList)dataTable.Rows[i].Cells[4].FindControl("ddlHours" + i)).SelectedItem.Value.ToString();
                mm[i] = ((DropDownList)dataTable.Rows[i].Cells[4].FindControl("ddlMinutes" + i)).SelectedItem.Value.ToString();
                type[i] = ((DropDownList)dataTable.Rows[i].Cells[4].FindControl("ddlType" + i)).SelectedItem.Value.ToString();
                datetime[i] = date[i].ToString() + " " + hh[i].ToString() + ":" + mm[i].ToString() + " " + type[i].ToString();
                dt[i] = Convert.ToDateTime(datetime[i]);
                strdt[i] = dt[i].ToString("yyyy-MM-dd HH:mm:ss");
                Response.Write(date[i]+"<br>");
                Response.Write(Convert.ToDateTime(date[i]) + "<br>");
                Response.Write(strdt[i] + "<br>");
            }
            
        }
    }
}
